import pandas as pd
import statsmodels.api as sm

# 载入数据
df = pd.read_csv('insurance.csv')

# 将类型变量转为数值变量
df['sex'] = df['sex'].map({'male': 0, 'female': 1})
df['smoker'] = df['smoker'].map({'yes': 1, 'no': 0})

# 定义自变量与因变量
X = df[['age', 'sex', 'bmi', 'children', 'smoker']]
Y = df['charges']

# 添加截距
X = sm.add_constant(X)

# 拟合线性回归模型
model = sm.OLS(Y, X)
results = model.fit()

# 输出结果
print(results.summary())

